package com.jjlin.chapter_2;

import java.util.*;
public class CD49{

    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    //建立单链表
    public static Node createNodeList(int[] arr){
        if(arr == null || arr.length < 1)
            return null;
        Node head = new Node(arr[0]);
        Node pre = head;
        for(int i = 1; i < arr.length; i++){
            Node cur = new Node(arr[i]);
            pre.next = cur;
            pre = cur;
        }
        return head;
    }

    //打印单链表
    public static void printNodeList(Node head){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //删除单链表倒数第k个节点
    public static Node removeLastKNode(Node head, int k){
        if(head == null || k < 1)
            return head;
        Node cur = head;
        while(cur != null){
            cur = cur.next;
            k--;
        }
        if(k == 0)
            head = head.next;
        else if(k < 0){
            cur = head;//删除节点
            Node pre = cur;//删除节点的前一个节点
            while(k++ != 0){//找到删除节点
                pre = cur;
                cur = cur.next;
            }
            pre.next = cur.next;
        }
        return head;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
            arr[i] = sc.nextInt();
        Node head = createNodeList(arr);
        head = removeLastKNode(head, k);
        printNodeList(head);
    }
}
